from socket import *
from threading import Thread
# 收数据
def recvData():
    while True:
        recvInfo = udpSocket.recvfrom(1024)
        message, addrInfo = recvInfo
        print(">>From:{}:{}".format(addrInfo, message.decode('gb2312')))


# 发送数据

def sendData():
    while True:
        sendInfo = input("<<")
        udpSocket.sendto(sendInfo.encode('gb2312'), (destIP, destPort))

udpSocket = None
destIP = ""
destPort = 0
def main():
    global udpSocket
    global destIP
    global destPort
    udpSocket = socket(AF_INET, SOCK_DGRAM)
    destIP = input("请输入IP地址：")
    destPort = int(input("请输入port端口："))
    udpSocket.bind((destIP, 8018))

    # 创建线程
    tr = Thread(target=recvData)
    ts = Thread(target=sendData)

    tr.start()
    ts.start()

    tr.join()
    ts.join()


if __name__ == "__main__":
    main()
